﻿//495.提莫攻击
class Solution {
public:
    int findPoisonedDuration(vector<int>& timeSeries, int duration) {
        int ans = duration;
        for (int i = 0; i < timeSeries.size() - 1; i++)
        {
            if (timeSeries[i] + duration <= timeSeries[i + 1])
            {
                ans += duration;
            }
            else
            {
                ans += timeSeries[i + 1] - timeSeries[i];
            }
        }

        return ans;
    }
};

//213.打家劫舍II
class Solution {
public:
    int rob(vector<int>& nums) {
        int n = nums.size();

        return max(nums[0] + rob1(nums, 2, n - 2), rob1(nums, 1, n - 1));
    }

    int rob1(vector<int>& nums, int left, int right)
    {
        if (left > right) return 0;
        int n = right - left + 1;
        vector<int> f(n), g(n);
        f[0] = nums[left];
        for (int i = 1; i < n; i++)
        {
            f[i] = g[i - 1] + nums[i + left];
            g[i] = max(g[i - 1], f[i - 1]);
        }
        return max(f[n - 1], g[n - 1]);
    }
};